/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package database.daos;

import database.orm.ORMException;
import database.databasemanager.exceptions.DataBaseException;
import beans.ClientHistoryBean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Fam. Tapia
 */
public class ClientHistoryDAO extends AbstractDAO<ClientHistoryBean>{



    @Override
    public void insert(ClientHistoryBean entidad) throws DAOException {
         try{
            dataBaseManager.openConnection();
            String sql = "INSERT INTO CLIENT_HISTORY(productAmount,clientHistoryDate, clientHistoryTime, clientHistoryTotal, userId, productID, storeID )values('"
                            + entidad.getProductAmount()+"','"
                            + entidad.getClientHistoryDate()+"','"
                            + entidad.getClientHistoryTime()+"','"
                            + entidad.getClientHistoryTotal()+"','"
                            + entidad.getUserId() + "','"
                            + entidad.getProductId() + "','"
                            + entidad.getStoreId() + "');";
           dataBaseManager.executeUpdate(sql);
        } catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }
       
    

    @Override
    public void delete(String condicion) throws DAOException {
        try{
            dataBaseManager.openConnection();
            String sql = "DELETE FROM client_history " + condicion;
            dataBaseManager.executeUpdate(sql);
            } catch (DataBaseException exception) {
               throw new DAOException(exception.getExceptionMessage(),exception);
            } finally{
                try {
                    dataBaseManager.closeConnection();
                } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
                }
            }
    }

    @Override
    public void update(ClientHistoryBean clientHistory, String condition) throws DAOException {
        try {
            dataBaseManager.openConnection();
            String sql = "UPDATE CLIENT_HISTORY SET productAmount='" +  clientHistory.getProductAmount()+
                    "',clientHistoryDate='"+ clientHistory.getClientHistoryDate()+
                    "',clientHistoryTime='"+ clientHistory.getClientHistoryTime()+
                    "',clientHistoryTotal='"+ clientHistory.getClientHistoryTotal()+
                    "',userId='"+ clientHistory.getUserId()+
                    "',productID='"+clientHistory.getProductId()+
                    "',storeID='"+ clientHistory.getStoreId()+"' "+condition;
            dataBaseManager.executeUpdate(sql);   
            } catch (DataBaseException exception) {
                throw new DAOException(exception.getExceptionMessage(),exception);
            }  finally{
                try {
                    dataBaseManager.closeConnection();
                } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
                }
            }
             
    }

    @Override
    protected ArrayList<ClientHistoryBean> searchAll(String condition) throws DAOException {
        ArrayList<ClientHistoryBean> clientHistoryBeanList = null;
        try {
            dataBaseManager.openConnection();
            String sqlStatement = "SELECT * FROM CLIENT_HISTORY "+condition;
            clientHistoryBeanList = createClientHistoryBeansList(dataBaseManager.executeQuery(sqlStatement));
            return clientHistoryBeanList;
        } catch (DataBaseException exception) {
             throw new DAOException(exception.getExceptionMessage(),exception);
        }  finally{
                try {
                    dataBaseManager.closeConnection();
                } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
                }
            }
        
    }

   
    @Override
    protected ClientHistoryBean search(String condition) throws DAOException {
       ClientHistoryBean clientHistoryBean = null;
       try{
            dataBaseManager.openConnection();
            String sqlStatement = "SELECT * FROM CLIENT_HISTORY " + condition;
            clientHistoryBean = createClientHistoryBean(dataBaseManager.executeQuery(sqlStatement));
            return clientHistoryBean;
           } catch (DataBaseException exception) {
               throw new DAOException(exception.getExceptionMessage(),exception);
           }  finally{
                try {
                    dataBaseManager.closeConnection();
                } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
                }
            }
    }

     private ArrayList<ClientHistoryBean> createClientHistoryBeansList(ResultSet queryResult) throws DAOException{
        ArrayList<ClientHistoryBean> clientHistoryBeansList = new ArrayList();
         try {
            while (queryResult.next()) {
                clientHistoryBeansList.add((ClientHistoryBean) beansGenerator.generateBean(queryResult, ClientHistoryBean.class));
            }
            return clientHistoryBeansList;
        } catch(ORMException exception)  {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } catch (SQLException exception) {
            throw new DAOException(exception.getMessage(),exception);
        } 
    }

    private ClientHistoryBean createClientHistoryBean(ResultSet queryResult) throws DAOException{
        try {
            moveQueryResultToBegin(queryResult);
            return (ClientHistoryBean) beansGenerator.generateBean(queryResult, ClientHistoryBean.class);
        } catch (ORMException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } catch (SQLException exception) {
            throw new DAOException(exception.getMessage(),exception);
        }
    }


    public ClientHistoryBean getClientHistory(int userId) throws DAOException{
       return search("WHERE userId='"+userId+"';");
    }

     public void moveQueryResultToBegin(ResultSet queryResult) throws SQLException{
        queryResult.first();
    }



}
